<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="应用编号" prop="appCode">
        <el-input v-model="queryParams.appCode" placeholder="请输入应用编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="应用名称" prop="appName">
        <el-input v-model="queryParams.appName" placeholder="请输入应用名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="small" @click="handleAdd"
          v-hasPermi="['application:applicationInfoData:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="small" :disabled="single" @click="handleUpdate"
          v-hasPermi="['application:applicationInfoData:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="small" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['application:applicationInfoData:remove']">删除</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="applicationInfoDataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="应用编码" align="center" prop="appCode" />
      <el-table-column label="应用类型" align="center" prop="appType" />
      <el-table-column label="应用名称" align="center" prop="appName" />
      <el-table-column label="屏蔽地段" align="center" prop="outCityArray" />
<!--      <el-table-column label="状态" align="center" prop="status">-->
<!--        <template slot-scope="scope">-->
<!--          <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0"-->
<!--            @change="handleStatusChange(scope.row, $event)"></el-switch>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="small" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['application:applicationInfoData:edit']">修改</el-button>
          <el-button size="small" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['application:applicationInfoData:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改应用包管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="应用编码" prop="appCode">
          <el-input v-model="form.appCode" placeholder="请输入应用编码" />
        </el-form-item>
        <el-form-item label="应用名称" prop="appName">
          <el-input v-model="form.appName" placeholder="请输入应用名称" />
        </el-form-item>
        <el-form-item label="屏蔽地段" prop="outCityArray">
          <el-select v-model="form.outCityArray" clearable placeholder="请选择屏蔽地段" filterable multiple>
            <el-option v-for="(item, i) in extData.address || []" :key="i" :label="item.key"
              :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="公司主体" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入公司主体名称" />
        </el-form-item>
        <el-form-item label="公司地址" prop="companyAddress">
          <el-input v-model="form.companyAddress" placeholder="请输入公司主体地址名称" />
        </el-form-item>

        <el-form-item label="文本" prop="textVersion">
          <el-input type="textarea" v-model="form.textVersion" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listApplicationInfoData, getApplicationInfoData, delApplicationInfoData, addApplicationInfoData, updateApplicationInfoData, configInfoExtData } from "@/api/application/applicationInfoData";

export default {
  name: "ApplicationInfoData",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 应用包管理表格数据
      applicationInfoDataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        appCode: null,
        appType: null,
        appName: null,
        extUrl: null,
        h5Url: null,
        wwwUrl: null,
        sortWeight: null,
        outCityArray: []
      },
      extData: {
        address: []
      },
      // 表单参数
      form: {
        outCityArray: ''
      },
      // 表单校验
      rules: {
        appCode: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
        appType: [
          { required: true, message: "$comment不能为空", trigger: "change" }
        ],
        appName: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
        extUrl: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
        h5Url: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
        wwwUrl: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
        sortWeight: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
      }

    };
  },
  created() {
    this.getList();
    this.getOptions();
  },
  methods: {

    /** 查询应用包管理列表 */
    getList() {
      this.loading = true;
      listApplicationInfoData(this.queryParams).then(response => {
        this.applicationInfoDataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getOptions() {
      configInfoExtData().then((response) => {
        this.extData = response.data;
        console.log(this.extData)
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        appCode: null,
        appType: null,
        appName: null,
        extUrl: null,
        h5Url: null,
        wwwUrl: null,
        sortWeight: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加应用包管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getApplicationInfoData(id).then(response => {
        this.form = response.data;
        this.form.outCityArray = JSON.parse(response.data.outCityArray || '[]')
        this.open = true;
        this.title = "修改应用包管理";
      });
    },
     handleStatusChange(row, value) {
      let text = (+value) === 0 ? "停用" : "启用";
      updateApplicationInfoData({ id: row.id, status: +value }).then(res => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(() => {
        row.status = row.status == "0" ? "1" : "0";
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          const payload = {
            ...this.form,
            outCityArray: JSON.stringify(this.form.outCityArray.map(item => item.toString()))
          }
          if (this.form.id != null) {
            updateApplicationInfoData(payload).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addApplicationInfoData(payload).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除应用包管理编号为"' + ids + '"的数据项？').then(function () {
        return delApplicationInfoData(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('application/applicationInfoData/export', {
        ...this.queryParams
      }, `applicationInfoData_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
